/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.hnilica.cisteniulic.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.hnilica.cisteniulic.dao.InfoDAO;
import net.hnilica.cisteniulic.dao.UliceDAO;
import net.hnilica.cisteniulic.dto.Info;
import net.hnilica.cisteniulic.dto.Ulice;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 *
 * @author swosh
 */
public class NewUlice extends HttpServlet {
private static final String url = "/sprava/index.jspx?akce=sprava";
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String nazUliceUser = request.getParameter("nazUlice").trim();

        Subject subject = SecurityUtils.getSubject();
        //principal is email in database
        String principal = (String) subject.getPrincipal();
        Info newInfo;
        Info oldInfo;
        Ulice ulice;
        boolean isUliceInDB = false;
        boolean isSameInfoInDB = false;
        boolean isSaved = false;
        request.setAttribute("chybasulice", "");
        request.setAttribute("uspechsulice", "");
        request.setAttribute("chyba", "");

        subject.checkRole("uzivatel");

        //   whether ulice is in database
        UliceDAO uliceDAO = new UliceDAO();
        try {
            ulice = uliceDAO.getUlice(nazUliceUser);
            if (ulice.getNazUlice().equals(nazUliceUser)) {
                isUliceInDB = true;
            }
        } catch (NullPointerException e) {
            request.setAttribute("chybasulice", "Ulice " + nazUliceUser + " není v seznamu. Možná jste udělal/a jen překlep.");
            isUliceInDB = false;
        }

        if (isUliceInDB) {
            //    whether does same relation between street and user exist
            InfoDAO infoDAO = new InfoDAO();
            try {
                oldInfo = infoDAO.getInfo(principal, nazUliceUser);
                if (oldInfo.getEmail().equals(principal) && oldInfo.getNazUlice().equals(nazUliceUser)) {
                    isSameInfoInDB = true;
                }
            } catch (NullPointerException e) {
                isSameInfoInDB = false;
            }

            //So what to do. Save or not save
            if (isSameInfoInDB) {
                request.setAttribute("chybasulice", "Ulici " + nazUliceUser + "už sledujete. Není třeba zadávat dvakrát.");
            } else {
                newInfo = new Info(principal, nazUliceUser);
                isSaved = infoDAO.saveOrUpdateInfo(newInfo);
                if (isSaved) {
                    request.setAttribute("uspechsulice", "Ulice " + nazUliceUser + " byla přidána do seznamu Vámi sledovaných ulic.");
                } else {
                    request.setAttribute("chyba", " Něco se nepovedlo při ukládání. Zkuste to znova.");
                }


            }
        }


        RequestDispatcher dispatcher = request.getRequestDispatcher(url);
        dispatcher.forward(request, response);


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/xhtml+xml;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        processRequest(request, response);

    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
